<?php

class ChangePasswordForm extends CFormModel {

    public $old_password;
    public $new_password;
    public $confirm_password;

    public function rules() {
        return array(
            array('old_password, new_password, confirm_password', 'required', 'message' => t('MSG_REQUIRED', 'messages')),
            array('old_password, new_password, confirm_password', 'length', 'min' => '6', 'tooShort' => t('MSG_STRING_TOO_SHORT', 'messages')),
            array('old_password, new_password, confirm_password', 'length', 'max' => '30', 'tooLong' => t('MSG_STRING_TOO_LONG', 'messages')),
            array('confirm_password', 'compare', 'compareAttribute' => 'new_password', 'message' => t('MSG_WRONG_CONFIRM_PASSWORD', 'messages')),
            array('old_password', 'validateComapare'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'old_password' => t('LBL_USER_OLD_PASSWORD', 'dashboard'),
            'new_password' => t('LBL_USER_NEW_PASSWORD', 'dashboard'),
            'confirm_password' => t('LBL_USER_CONFIRM_PASSWORD', 'dashboard'),
        );
    }

    public function validateComapare($attribute, $params) {
        $id = Yii::app()->user->id;
        $user = User::model()->findByPk($id);
        $oldPassword = hashPassword($this->$attribute);
        if ($user->password != $oldPassword || empty($user->password))
            $this->addError($attribute, t('MSG_INCORRECT', 'messages', array('{0}' => 'Old password')));
    }

}

